// thread.h

#ifndef __thread_h__
#define __thread_h__

#include "common.h"
#include <iostream>
#include <boost/thread.hpp>  
#include <boost/date_time.hpp>  

namespace pilot { namespace thread
{
class Worker1
{  
public:   
       
    Worker1(unsigned N, float guess, unsigned iter)   
            : m_Number(N),  
              m_Guess(guess),  
              m_Iterations(iter)  
    {  
    }  
       
    void operator()()  
    {  
        std::cout << "Worker1: calculating sqrt(" << m_Number  
                  << "), itertations = "  
                  << m_Iterations << std::endl;  
           
        // Use Newton's Method  
        float   x;  
        float   x_last = m_Guess;  
           
        for (unsigned i=0; i < m_Iterations; i++)  
        {  
            x = x_last - (x_last*x_last-m_Number)/  
                    (2*x_last);  
            x_last = x;  
               
            std::cout << "Iter " << i << " = "  
                  << x << std::endl;  
        }  
           
        std::cout << "Worker1: Answer = " << x << std::endl;  
    }  
   
private:  
   
    unsigned    m_Number;  
    float       m_Guess;  
    unsigned    m_Iterations;  
};  
	class Worker
	{
	public:
		void operator()()
		{
			std::cout << "pilot::thread::Worker::operator(): TID=" << boost::this_thread::get_id() << std::endl;
		}
	};

	void WorkerFunc(int a, int b)  
	{  
		std::cout << "pilot::thread::Worker::WorkerFunc: TID=" << boost::this_thread::get_id() << std::endl;
	}  
  
	int test(int count = 10)
	{  
		//boost::this_thread::sleep(boost::posix_time::seconds(1));
		std::cout << "pilot::thread::test: startup" << std::endl;  
		std::cout << "pilot::thread::test: " << boost::this_thread::get_id() << std::endl;

		// sample 1
		{
			boost::thread thd(WorkerFunc, 1, 2);
			thd.join();
		}

		// sample 2
		{
			Worker w;
			boost::thread thd(w);
			//boost::thread workerThread(&worker::process, &w, 3);
			//boost::thread workerThread(worker::process);
       
			std::cout << "pilot::thread::test: waiting for thread, PID=" << boost::this_thread::get_id() << std::endl;
       
			thd.join();
		}
       
		std::cout << "pilot::thread::test: done" << std::endl;  
       
		return 0;  
	}  
	//boost::threadpool::pool tp(num_threads);
}}

#endif //__thread_h__
